package demo2;
//接口的实现场景之一
//用一个特殊的接口，Comparable

class Student implements Comparable<Student> {  //这是以后用到的泛型，此时先不用理解
    public String name;
    public int age;
    public int score;

    //带有参数的构造方法
    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    //进行重写toString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    //进行重写compareTo方法
    @Override
    public int compareTo(Student o) {
       /* if(this.age > o.age ) {
            return 1;
        }else if(this.age < o.age) {
            return -1;
        }else {
            return 0;
        }
    }*/
        //可以直接返回的,更加简洁也是没有问题的
        return this.age - o.age;
    }
}

public  class Test {
    public static void main(String[] args) {
        //int[] array = {};
        //表示Student类型的数组，就和int[]一样，类型是int
        Student[] student = new Student[3];  //此时有了三个学生对象
        student[0] = new Student("小白", 18, 90);  //当新建对象时，会调用有参数的构造方法，所以要传递参数
        student[1] = new Student("小灰", 19, 95);
        student[2] = new Student("小黑", 20, 91);
        //按照年龄进行排序
    }

}
